package com.fr.base.xml;

import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreUtils;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.LinkedList;
import java.util.logging.Logger;

public class XMLPrintWriter
{
  public static final String XML_ENCODER = "UTF-8";
  public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  public static final String NEW_LINE = "\n";
  private boolean rootPrinted;
  private LinkedList tagChain = new LinkedList();
  private boolean isLastTagOpened = false;
  private PrintWriter writer;
  private boolean debug = false;
  private boolean wellFormatted = true;

  public static XMLPrintWriter create(OutputStream paramOutputStream)
  {
    return create(paramOutputStream, true);
  }

  public static XMLPrintWriter create(OutputStream paramOutputStream, boolean paramBoolean)
  {
    PrintWriter localPrintWriter;
    try
    {
      localPrintWriter = new PrintWriter(new OutputStreamWriter(paramOutputStream, "UTF-8"));
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      localPrintWriter = new PrintWriter(new OutputStreamWriter(paramOutputStream));
    }
    return create(localPrintWriter, paramBoolean);
  }

  public static XMLPrintWriter create(Writer paramWriter)
  {
    return create(paramWriter, true);
  }

  public static XMLPrintWriter create(Writer paramWriter, boolean paramBoolean)
  {
    return create(new PrintWriter(paramWriter), paramBoolean);
  }

  public static XMLPrintWriter create(PrintWriter paramPrintWriter)
  {
    return create(paramPrintWriter, true);
  }

  public static XMLPrintWriter create(PrintWriter paramPrintWriter, boolean paramBoolean)
  {
    return new XMLPrintWriter(paramPrintWriter, paramBoolean);
  }

  private XMLPrintWriter(PrintWriter paramPrintWriter, boolean paramBoolean)
  {
    this.writer = paramPrintWriter;
    this.wellFormatted = paramBoolean;
  }

  private void export(String paramString)
  {
    this.writer.print(paramString);
    if (this.debug)
      FRContext.getLogger().info(paramString);
  }

  public XMLPrintWriter startTAG(String paramString)
  {
    if (!(this.rootPrinted))
      return startRoot(paramString);
    closeLastTagOfChain();
    export("<" + paramString);
    this.tagChain.add(paramString);
    this.isLastTagOpened = true;
    return this;
  }

  private XMLPrintWriter startRoot(String paramString)
  {
    export("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    if (this.wellFormatted)
      export("\n");
    this.rootPrinted = true;
    startTAG(paramString).attr("xmlVersion", 20100901L);
    return this;
  }

  public XMLPrintWriter textNode(String paramString)
  {
    closeLastTagOfChain();
    export(BaseCoreUtils.join(new String[] { "<![CDATA[", XMLEncodeUtils.cdataEncode(paramString), "]]>" }));
    return this;
  }

  private void closeLastTagOfChain()
  {
    if (this.isLastTagOpened)
    {
      export(">");
      if (this.wellFormatted)
        export("\n");
      this.isLastTagOpened = false;
    }
  }

  public XMLPrintWriter attr(String paramString, long paramLong)
  {
    return attr(paramString, String.valueOf(paramLong));
  }

  public XMLPrintWriter attr(String paramString, boolean paramBoolean)
  {
    return attr(paramString, String.valueOf(paramBoolean));
  }

  public XMLPrintWriter attr(String paramString, int paramInt)
  {
    return attr(paramString, String.valueOf(paramInt));
  }

  public XMLPrintWriter attr(String paramString, double paramDouble)
  {
    return attr(paramString, String.valueOf(paramDouble));
  }

  public XMLPrintWriter attr(String paramString1, String paramString2)
  {
    if (paramString2 == null)
      return this;
    export(BaseCoreUtils.join(new String[] { " ", paramString1, "=\"", BaseXMLUtils.xmlAttrEncode(paramString2), "\"" }));
    return this;
  }

  public XMLPrintWriter end()
  {
    String str = (String)this.tagChain.removeLast();
    if (this.isLastTagOpened)
    {
      export("/>");
      if (this.wellFormatted)
        export("\n");
    }
    else
    {
      export("</" + str + ">");
      if (this.wellFormatted)
        export("\n");
    }
    this.isLastTagOpened = false;
    return this;
  }

  public void flush()
  {
    this.writer.flush();
  }

  public void close()
  {
    if (this.tagChain.size() > 0)
    {
      String[] arrayOfString = (String[])this.tagChain.toArray(new String[this.tagChain.size()]);
      throw new RuntimeException(BaseCoreUtils.join(new String[] { BaseCoreUtils.join(arrayOfString, ","), (arrayOfString.length > 1) ? "are" : "is", "not closed." }, " "));
    }
    this.writer.close();
  }
}